package com.example.plus5;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;

import android.R.color;
import android.os.Bundle;
import android.widget.TableRow.LayoutParams;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

@SuppressLint({ "ResourceAsColor", "NewApi" })
public class Appointments extends Activity {

	private SharedPreferences appointmentsEntered = null;
	private ArrayList<appointment> allAppointments = new ArrayList<appointment>();
	private ArrayList<appointment> unupdatedList = new ArrayList<appointment>();
	private TableLayout appointmentRows;
	appointment newAppointmentIntent = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appointments);

		appointmentsEntered = getSharedPreferences("appointmentList", MODE_PRIVATE);
		

		
		Intent intent = getIntent();
		setContentView(R.layout.activity_appointments);
		appointmentRows = (TableLayout) findViewById(R.id.appointmentRows);

		
		newAppointmentIntent = (appointment) getIntent().getSerializableExtra("New_Appointment");		
		updateAppList(newAppointmentIntent);
						
	}

	
	public void updateAppList(appointment appointmentToAdd) {
		// TODO Auto-generated method stub
		newAppointmentIntent = null;
		if (appointmentToAdd == null){
			if( appointmentsEntered.getStringSet("appointmentList", null) != null){
	
				allAppointments = retrieveAppointmentList();
				Collections.sort(allAppointments);
				
				for(appointment s:allAppointments){
				insertAppointmentInScrollView(s);
				}
			}
		}else{
			if(allAppointments.indexOf(appointmentToAdd)==-1){
			
				if( appointmentsEntered.getStringSet("appointmentList", null) != null){

					unupdatedList = retrieveAppointmentList();
				}	
					
				unupdatedList.add(appointmentToAdd);
					
				saveAppointmentList(unupdatedList);
					
				allAppointments = retrieveAppointmentList();
				
				
				Collections.sort(allAppointments);
				
				for(appointment s:allAppointments){
				insertAppointmentInScrollView(s);
				}
			}
		}
	}
	
	
		

	private void saveAppointmentList(ArrayList<appointment> newAppointmentArrayList) {
		// TODO Auto-generated method stub
		Set<String> set = new HashSet<String>();
	   
		for (int i = 0; i < newAppointmentArrayList.size(); i++) {
		set.add(newAppointmentArrayList.get(i).getjSonObject().toString());
		
		}
		Editor editor = appointmentsEntered.edit();
		editor.putStringSet("appointmentList", set);
		editor.commit();
	}
	

	private ArrayList<appointment> retrieveAppointmentList() {
		// TODO Auto-generated method stub
		ArrayList<appointment> updatedListToReturn = new ArrayList<appointment>();
		Set<String> set = appointmentsEntered.getStringSet("appointmentList", null);
		for (String s : set) {
	        try {
	            JSONObject jsonObject = new JSONObject(s);
	            String name = jsonObject.getString("name");
	            String day = jsonObject.getString("day");
	            String time = jsonObject.getString("time");
	            String location = jsonObject.getString("location");
	            String enable = jsonObject.getString("enable");

	            appointment myAppointment = new appointment(name, location, day, time);

	            updatedListToReturn.add(myAppointment);

	        } catch (JSONException e) {
	            e.printStackTrace();
	        }
	    }
		
		return updatedListToReturn;
	}

	private void insertAppointmentInScrollView(appointment appointmentToAdd) {
/*		// TODO Auto-generated method stub
		TableLayout appointmentRows = (TableLayout) findViewById(R.id.appointmentRows);
		TableRow tr1 = new TableRow(this);
		TableRow tr2 = new TableRow(this);
		LayoutParams lp= new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		tr1.setLayoutParams(lp);
		tr2.setLayoutParams(lp);
		LayoutParams daylp = new LayoutParams();
		daylp.column = 1;
		daylp.span = 1;
		LayoutParams timelp = new LayoutParams();
		timelp.column = 1;
		timelp.span = 1;
		LayoutParams namelp = new LayoutParams();
		namelp.span = 1;
		namelp.column = 1;
		
		LayoutParams buttonDeleteLp = new LayoutParams();
		buttonDeleteLp.column =6;
		buttonDeleteLp.span = 1;		
		LayoutParams buttonEnableLp = new LayoutParams();
		buttonEnableLp.column =4;
		buttonEnableLp.span = 1;
		LayoutParams buttonLp = new LayoutParams();
		buttonEnableLp.column =5;
		buttonEnableLp.span = 1;		
		
		TextView appointmentDay = new TextView(this);
		appointmentDay.setLayoutParams(daylp);
		appointmentDay.setTextColor(R.color.plus5_red);
		appointmentDay.setText(appointmentToAdd.getDay());
		
		TextView appointmentTime = new TextView(this);
		appointmentTime.setLayoutParams(timelp);
		appointmentTime.setTextColor(R.color.plus5_red);
		appointmentTime.setText(appointmentToAdd.getTime());
		
		TextView appointmentName = new TextView(this);
		appointmentName.setLayoutParams(namelp);
		appointmentName.setTextColor(R.color.plus5_red);
		appointmentName.setText(appointmentToAdd.getName());		
		
		Button enableButton = new Button(this);
		enableButton.setLayoutParams(buttonEnableLp);
		enableButton.setBackgroundColor(R.color.plus5_black);
		enableButton.setText("Disable");
		enableButton.setTextColor(R.color.plus5_red);
		enableButton.setTextSize(33);

		
		Button editButton = new Button(this);
		editButton.setBackgroundResource(R.drawable.pencil48);
		editButton.setLayoutParams(buttonLp);
//		editButton.setPadding(4, 4, 4, 4);
		
		Button deleteButton = new Button(this);
		deleteButton.setBackgroundColor(R.color.plus5_black);
		deleteButton.setLayoutParams(buttonDeleteLp);
		deleteButton.setText("");
		deleteButton.setTextColor(R.color.plus5_red);
		deleteButton.setTextSize(33);
		
		tr1.addView(appointmentName);
		tr2.addView(appointmentDay);
		tr2.addView(appointmentTime);
		tr2.addView(enableButton);
		tr2.addView(editButton);
		tr2.addView(deleteButton);
		
		appointmentRows.addView(tr1, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		appointmentRows.addView(tr2, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
*/		
		LayoutInflater nameInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LayoutInflater appointmentInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View nameView = nameInflater.inflate(R.layout.new_appointment_namerow, null);
	    View appointmentView = appointmentInflater.inflate(R.layout.new_appointment_row, null);
	    
	    TextView appointmentNameRow = (TextView)nameView.findViewById(R.id.appointmentNameRow);
	    TextView appointmentDayRow = (TextView)appointmentView.findViewById(R.id.appointmentDayRow);
	    TextView appointmentTimeRow = (TextView)appointmentView.findViewById(R.id.appointmentTimeRow);
	    Button appointmentEnableButton = (Button) appointmentView.findViewById(R.id.appointmentEnableButton);
	    Button appointmentEditButton = (Button) appointmentView.findViewById(R.id.appointmentEditButton);
	    Button appointmentDeleteButton = (Button) appointmentView.findViewById(R.id.appointmentDeleteButton);
	    
	    appointmentNameRow.setText(appointmentToAdd.getName());
	    appointmentDayRow.setText(appointmentToAdd.getDay());
	    appointmentTimeRow.setText(appointmentToAdd.getTime());
	    
	    appointmentRows.addView(nameView);
	    appointmentRows.addView(appointmentView);
		
	}

	public void enterAppointmentAdd(View v){
		Intent intent = new Intent(Appointments.this, AddAppointment.class);
		startActivity(intent);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.appointments, menu);
		return true;
	}

}


